/**
 * @file    wifi_wss.c
 * @author  Azolla (1228449928@qq.com)
 * @brief   websocket ssl
 * @version 0.1
 * @date    2023-03-15
 * 
 * @copyright Copyright (c) 2023
 * */
#include "freertos/FreeRTOS.h"
#include "freertos/event_groups.h"
#include "freertos/task.h"
#include "freertos/timers.h"
#include "freertos/queue.h"  
#include "cJSON.h"
#include "sys/time.h"
#include "time.h"

#include "esp_websocket_client.h"
#include "esp_event.h"
#include "esp_log.h"

 
#define TAG  "wifi_wss"


static void websocket_event_handler(void *handler_args, esp_event_base_t base, int32_t event_id, void *event_data)
{
    esp_websocket_event_data_t *data = (esp_websocket_event_data_t *)event_data;
    switch (event_id) {
    case WEBSOCKET_EVENT_CONNECTED:
        ESP_LOGI(TAG, "WEBSOCKET_EVENT_CONNECTED");
        break;
    case WEBSOCKET_EVENT_DISCONNECTED:
        ESP_LOGI(TAG, "WEBSOCKET_EVENT_DISCONNECTED");
        break;
    case WEBSOCKET_EVENT_DATA:
        ESP_LOGI(TAG, "WEBSOCKET_EVENT_DATA");
        ESP_LOGI(TAG, "Received opcode=%d", data->op_code);
        if (data->op_code == 0x08 && data->data_len == 2) {
            ESP_LOGW(TAG, "Received closed message with code=%d", 256*data->data_ptr[0] + data->data_ptr[1]);
        } else {
            ESP_LOGW(TAG, "Received=%.*s", data->data_len, (char *)data->data_ptr);
        }
        ESP_LOGW(TAG, "Total payload length=%d, data_len=%d, current payload offset=%d\r\n", data->payload_len, data->data_len, data->payload_offset);
        break;
    case WEBSOCKET_EVENT_ERROR:
        ESP_LOGI(TAG, "WEBSOCKET_EVENT_ERROR");
        break;
    default:
        ESP_LOGI(TAG, "websocket_event_handler default = %d", event_id);
        break;    
    }
}

void wss_read_task(void *arg)
{
    esp_websocket_client_handle_t client = (esp_websocket_client_handle_t)arg;
    char data[32];
    int i = 0;
    while (true) {
        if (esp_websocket_client_is_connected(client)) {
            int len = sprintf(data, "hello %04d", i++);
            ESP_LOGI(TAG, "Sending %s", data);
            esp_websocket_client_send_text(client, data, len, portMAX_DELAY);
        } else {
            ESP_LOGW(TAG, "not connected....");
        }
        vTaskDelay(3000 / portTICK_PERIOD_MS);
    }
}

// root.crt client.key client.crt
// extern const char cacert_pem_start[] asm("_binary_root_crt_start");
// extern const char cacert_pem_end[]   asm("_binary_root_crt_end");

// extern const char prvtkey_pem_start[] asm("_binary_client_key_start");
// extern const char prvtkey_pem_end[]   asm("_binary_client_key_end");

// extern const char client_pem_start[] asm("_binary_client_crt_start");
// extern const char client_pem_end[]   asm("_binary_client_crt_end");

// websocket ssl init...
void wifi_wss_init(void)
{
    esp_log_level_set("WEBSOCKET_CLIENT", ESP_LOG_DEBUG);
    esp_log_level_set("TRANSPORT_WS", ESP_LOG_DEBUG);
    esp_log_level_set("TRANS_TCP", ESP_LOG_DEBUG);
 
    esp_websocket_client_config_t websocket_cfg = { };
    websocket_cfg.buffer_size = 1030;
    // websocket_cfg.cert_pem = cacert_pem_start;
    // websocket_cfg.cert_len = cacert_pem_end - cacert_pem_start;
    // websocket_cfg.client_key = prvtkey_pem_start;
    // websocket_cfg.client_key_len = prvtkey_pem_end - prvtkey_pem_start;
    // websocket_cfg.client_cert = client_pem_start;
    // websocket_cfg.client_cert_len = client_pem_end - client_pem_start;
    websocket_cfg.transport = WEBSOCKET_TRANSPORT_OVER_SSL;
    websocket_cfg.keep_alive_enable = true;
    websocket_cfg.uri = "wss://192.168.2.103:8848";  // wss://192.168.2.103:8848

    ESP_LOGI(TAG, "Connecting to %s...", websocket_cfg.uri);

    esp_websocket_client_handle_t client = esp_websocket_client_init(&websocket_cfg);
    esp_websocket_register_events(client, WEBSOCKET_EVENT_ANY, websocket_event_handler, (void *)client);

    esp_websocket_client_start(client);
 
    xTaskCreatePinnedToCore(wss_read_task, "wss_read_task", 4 * 1024, &client, 10, NULL, APP_CPU_NUM); 
}

 









